package com.example.lhsama.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.lhsama.domain.Member;
import com.example.lhsama.domain.Orders;
import com.example.lhsama.domain.Product;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.core.annotation.Order;

import java.util.List;

/**
 * @Date: 2021年09月19日 15:06
 * @Author: LHSAMA
 */
@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {

    @Select("select * from orders")
    @Result(id = true,property = "id",column = "id")
    @Result(property = "member",column = "member_id",javaType = Member.class , one = @One(select = "com.example.lhsama.dao.MemberMapper.selectById",fetchType = FetchType.EAGER) )
    @Result(property = "products",column = "id" ,javaType = Product.class,many = @Many(select = "com.example.lhsama.dao.ProductMapper.findAllByOrdersId",fetchType = FetchType.LAZY))
    List<Orders> findAll();

    @Select("select num from orders_product where product_id = #{productId} and orders_id = #{ordersId}")
    Integer countProducts(Integer productId,Integer ordersId);

    @Select("select * from product where id in (" +
            "select product_id from orders_product where orders_id = #{id}" +
            ")")
    List<Product> findProductsByOrdersId(Integer id);

    @Select("select * from orders where id = #{id}")
    @Result(id = true,property = "id",column = "id")
    @Result(property = "member",column = "member_id",javaType = Member.class , one = @One(select = "com.example.lhsama.dao.MemberMapper.selectById",fetchType = FetchType.EAGER) )
    Orders findById(Integer id);
}
